<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">角色管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>角色管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <input id="role-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="role-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="role-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="role-table" lay-filter="role-table"></table>
    </div>
</div>

<!-- 表单弹窗 -->
<script type="text/html" id="role-model">
    <form id="role-form" lay-filter="role-form" class="layui-form model-form">
        <input name="roleId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">角色名</label>
            <div class="layui-input-block">
                <input name="id" class='layui-hide'/>
                <input name="name" placeholder="请输入角色名" type="text" class="layui-input" maxlength="20"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item model-form-footer">
            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            <button class="layui-btn" lay-filter="role-form-submit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="role-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var formSelects = layui.formSelects;

        var Role = {
            init : function(){
                 // 添加按钮点击事件
                $('#role-btn-add').click(function () {
                    Role.showEditModel();
                });
                  // 搜索按钮点击事件
                $('#role-btn-search').click(function () {
                    var keyword = $('#role-edit-search').val();
                    Role.reloadRoleTable();
                });
                Role.initRoleTable();

                Role.bindSubmitForm();
            },
            initRoleTable:function(){
                //渲染表格
                roleTable = table.render({
                    elem: '#role-table',
                    url: config.base_server+'service-system/roles',
                    where: {
                        access_token: config.getToken().access_token
                    },
                    height:'full-300',
                    page: true,
                    cols: [[
                        {type: 'numbers'},
                        {field: 'id', title: 'ID'},
                        {field: 'name',  title: '角色名'},
                        {field: 'remark', title: '备注'},
                        {
                            field: 'time_create' , title: '创建时间', templet: function (d) {
                                return util.toDateString(d.createTime);
                            }
                        },
                        {align: 'center', toolbar: '#role-table-bar', title: '操作'}
                    ]]
                    ,
                    parseData: function(res){ //res 即为原始返回的数据
                            return {
                              "code": '0', //解析接口状态
                              "msg": res.msg, //解析提示文本
                              "count": res.data.total, //解析数据长度
                              "data": res.data.records //解析数据列表
                            };
                          }
                });

                // 工具条点击事件
                table.on('tool(role-table)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'edit') { //修改
                        Role.showEditModel(data);
                    } else if (obj.event === 'del') { //删除
                        Role.deleteRole(data.id);
                    }
                });

            },
            reloadRoleTable:function(){
                roleTable.reload({
                    where:{
                        access_token: config.getToken().access_token,
                        keyword:$('#role-edit-search').val()
                    },
                    page:{
                        curr:1
                    }
                })
            },
            showEditModel:function(data){
                 layer.open({
                    type: 1,
                    title: data ? '修改角色' : '添加角色',
                    area: '450px',
                    offset: '120px',
                    content: $('#role-model').html(),
                    success: function () {
                        $('#role-form')[0].reset();
                        $('#role-form').attr('method', 'post');
                        if (data) {
                            form.val('role-form', data);
                            $('#role-form').attr('method', 'put');
                        }
                    }
                });
            },
            deleteRole:function(id){
                layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('service-system/role/' + id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 10000) {
                            layer.msg(data.msg, {icon: 1});
                            Role.reloadRoleTable();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, 'delete');
                });
            },
            bindSubmitForm : function(){
                // 表单提交事件
                form.on('submit(role-form-submit)', function (data) {
                    layer.load(2);
                    admin.req('service-system/role', data.field, function (data) {
                        layer.closeAll('loading');
                        if (data.code == '10000') {
                            layer.msg(data.msg, {icon: 1});
                            table.reload('role-table');
                            layer.closeAll('page');
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, $('#role-form').attr('method'));
                    return false;
                });
            }

        };
        
        Role.init();

    });

</script>